import Head from 'next/head'
import styles from '../styles/Home.module.css'
import Header from "../components/Header"
import MyList from "../components/List"
import { Affix } from 'antd'
import { useState } from 'react'
import axios from "axios"

const Search=(props) =>{
    //把搜索的文章对象变成数组的形式
    let data = [];
    for(let i in props){
        data[i] = props[i]
    }
  const [top, setTop] = useState(0);
  const [list,setList] = useState(data)
  return (
    <div className={styles.container}>
      <Head>
        <title>blog</title>
        <meta name="description" content="Generated by create next app" />
        <link rel="icon" href="/favicon.ico" />
      </Head>
      <Affix offsetTop={top}>
      <Header/>
      </Affix >
      <MyList list={list}/>
    </div>
    
  )

}

Search.getInitialProps = async (context)=>{
    let title = context.query.title
    console.log(title,'title');
    const p = new Promise((resolve) => {
        axios("http://127.0.0.1:7001/default/searchArticle/"+title)
            .then((res) => {
                console.log(res,'res');
                resolve(res.data.data)
            })
    })
    return await p;
}

export default Search;